package cn.iocoder.yudao.module.wine.controller.admin.supplytype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplytype.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplytype.SupplyTypeDO;
import cn.iocoder.yudao.module.wine.service.supplytype.SupplyTypeService;

@Tag(name = "管理后台 - 酒类类型")
@RestController
@RequestMapping("/wine/supply-type")
@Validated
public class SupplyTypeController {

    @Resource
    private SupplyTypeService supplyTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建酒类类型")
    @PreAuthorize("@ss.hasPermission('wine:supply-type:create')")
    public CommonResult<Integer> createSupplyType(@Valid @RequestBody SupplyTypeSaveReqVO createReqVO) {
        return success(supplyTypeService.createSupplyType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新酒类类型")
    @PreAuthorize("@ss.hasPermission('wine:supply-type:update')")
    public CommonResult<Boolean> updateSupplyType(@Valid @RequestBody SupplyTypeSaveReqVO updateReqVO) {
        supplyTypeService.updateSupplyType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除酒类类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-type:delete')")
    public CommonResult<Boolean> deleteSupplyType(@RequestParam("id") Integer id) {
        supplyTypeService.deleteSupplyType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得酒类类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-type:query')")
    public CommonResult<SupplyTypeRespVO> getSupplyType(@RequestParam("id") Integer id) {
        SupplyTypeDO supplyType = supplyTypeService.getSupplyType(id);
        return success(BeanUtils.toBean(supplyType, SupplyTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得酒类类型列表")
    @PreAuthorize("@ss.hasPermission('wine:supply-type:query')")
    public CommonResult<List<SupplyTypeRespVO>> getSupplyTypePage(@Valid SupplyTypePageReqVO pageReqVO) {
        List<SupplyTypeDO> list = supplyTypeService.getSupplyTypeList(pageReqVO);
        list.sort(Comparator.comparing(SupplyTypeDO::getSort));
        return success(BeanUtils.toBean(list,SupplyTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出酒类类型 Excel")
    @PreAuthorize("@ss.hasPermission('wine:supply-type:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplyTypeExcel(@Valid SupplyTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplyTypeDO> list = supplyTypeService.getSupplyTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "酒类类型.xls", "数据", SupplyTypeRespVO.class,
                        BeanUtils.toBean(list, SupplyTypeRespVO.class));
    }

}
